package BehavioralPattern.Mediator;


/**
 * @Classname : Mediator
 * @Description : 中介者角色
 * @Author : chentianyu
 * @Date 2023/3/5 20:43
 */

// 抽象中介者
public abstract class Mediator {
    protected Purchase purchase;
    protected Stock stock;
    protected Sale sale;

    public void setPurchase(Purchase purchase) {
        this.purchase = purchase;
    }

    public void setStock(Stock stock) {
        this.stock = stock;
    }

    public void setSale(Sale sale) {
        this.sale = sale;
    }

    // 核心协调方法
    public abstract void coordinate(String action, Object...objects);
}


// 具体中介者
class ConcreteMediator extends Mediator {

    @Override
    public void coordinate(String action, Object...objects) {
        switch (action) {
            case "purchase":
                System.out.println("采购部：采购商品" + objects[0] + "件");
                super.stock.increase((Integer) objects[0]);  // 库存增加
                if (super.stock.highStock()) {
                    System.out.println("------高库存，商品促销------");
                    this.coordinate("sale", super.stock.getStockNum() / 2);
                }
                break;
            case "sale":
                System.out.println("销售部：销售商品" + objects[0] + "件");
                super.stock.decrease((Integer) objects[0]);  // 库存减少
                if (super.stock.lowStock()) {
                    System.out.println("------低库存，采购商品------");
                    this.coordinate("purchase", 500);
                }
                break;
        }
    }

}
